package benny.ApkposStore.Activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.GridView;
import benny.ApkposStore.R;
import benny.ApkposStore.Adapter.RoomAdapter;
import benny.ApkposStore.Adapter.RoomAdapter.ViewHolder;
import benny.ApkposStore.utils.ApkConfigdbUntils;
import benny.ApkposStore.utils.ApkUntils;
import benny.ApkposStore.utils.Apkglobal;

//import com.tencent.elife.asyn.BaseManager;
//import com.tencent.elife.asyn.Command;
//import com.tencent.elife.asyn.DataResponse;

public class RoomListActivity extends Activity {

	private GridView roomGridView;
	private int room_heightdip;
	private final int room_heightpx = 110;
	private int verticalSpacingdp;
	private final int verticalSpacing = 20;
	private int roomview_height;
	private List<HashMap<String, String>> roomlist = null;
	private RoomAdapter roomAdapter = null;
	private String room_name;
	private String room_id;
	private int price_type;
	private SQLiteDatabase db;
	private Button closebtn;
	private static Context mcx;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ApkUntils.fullscreen(this);
		setContentView(R.layout.roomlist);
		// 设置并显示房台区域
		mcx=this;
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();// 根据bundle的key得到对应的对象
		room_name = bundle.getString("room_name");
		room_id = bundle.getString("room_id");
		price_type= bundle.getInt("price_type");  
		closebtn = (Button) findViewById(R.id.closebtn); // 取得组件
		closebtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish(); // 关闭弹出窗口
			}
		});
		// ////////////////////////////////////////
		roomGridView = (GridView) findViewById(R.id.roomGridview);
		room_heightdip = ApkUntils.convertPX2DIP(this, room_heightpx
				* ApkUntils.getPixelsintH(this));
		verticalSpacingdp = ApkUntils.convertPX2DIP(this, verticalSpacing
				* ApkUntils.getPixelsintH(this));
		roomGridView.setNumColumns(3);
		roomGridView.setGravity(Gravity.FILL_VERTICAL);
		roomGridView.setSelector(R.color.transparent);
		roomGridView.setVerticalSpacing(verticalSpacingdp);
		roomlist=ApkConfigdbUntils.updateroomflag(mcx, room_id);
		roomAdapter = buildRoomAdapter(roomlist);
		updateroomGridView();
		roomGridView.setOnItemClickListener(new roomitemListener());
		roomGridView.setOnItemLongClickListener(new roomitemlongListener());
		
	}
	
	// 创建room长按Listener
		class roomitemlongListener implements OnItemLongClickListener {
			ViewHolder holder = null;

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				holder = (ViewHolder) arg1.getTag();
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("room_name", holder.roomtitle.getText().toString());
				bundle.putString("room_id", holder.roomtitle.getHint().toString());
				bundle.putInt("price_type", holder.price_type);
				intent.putExtras(bundle);		
				intent.setClass(RoomListActivity.this, RoomoperateActivity.class);			
				startActivity(intent);
				finish();				

				return false;
			}

		}

	
	// 创建一个 RoomAdapter
		private RoomAdapter buildRoomAdapter(List<HashMap<String, String>> roomlist) {
			List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
			for (Iterator<HashMap<String, String>> iterator = roomlist.iterator(); iterator.hasNext();) {
				HashMap<String, String> room = (HashMap<String, String>) iterator.next();
				HashMap<String, Object> map = new HashMap<String, Object>();
				String Rstate=room.get("room_state");
				String Payment=room.get("dish_total");
				float ddd = (float) (Float.parseFloat(Payment)*0.01);
				Payment=ApkUntils.convertFloat(ddd+"", 1);
				String Time=room.get("open_time");
				String flag=room.get("flag");
				Time=Time.substring(Time.indexOf(" ") + 1, Time.length());
				map.put("room_name", room_name+flag);
				map.put("room_id", room_id);
				map.put("room_state", Rstate);
				//map.put("room_floor", room.getFloor_id());
				map.put("price_type", price_type);		
				map.put("room_cnt", "0");	
				map.put("room_time", Time);
				map.put("room_payment",Payment);
				map.put("orderid",room.get("orderid"));
		
				if (Rstate.equals("1") || Rstate.equals("3")) {
					map.put("icon", R.drawable.selector_room_yellow);
				} else if (Rstate.equals("6")) {
					map.put("icon", R.drawable.selector_room_blue);
				} else {
					map.put("icon", R.drawable.selector_room_green);
				}

			list.add(map);
			}
			
			RoomAdapter roomadapter = new RoomAdapter(this, list);
			return roomadapter;

		}
	
	
	// 刷新房台界面
	public void updateroomGridView() {
		roomview_height = room_heightdip
				* (int) Math.ceil(roomAdapter.getCount() / 3.0);
		if (Apkglobal.getcurrentapiVersion() > 10) {
			roomGridView.setAdapter(roomAdapter);
		}
		ApkUntils.changeviewlayout(roomGridView, 0, roomview_height,
				roomAdapter);
		// roomAdapter.notifyDataSetChanged();
	}
	
	class roomitemListener implements OnItemClickListener {
		ViewHolder holder = null;
		private String orderid;
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			holder = (ViewHolder) view.getTag();
			orderid = holder.orderid;
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("orderid", orderid);
			bundle.putString("room_name", holder.roomtitle.getText().toString());
			bundle.putString("room_id", holder.roomtitle.getHint().toString());
			bundle.putInt("people_num", 1);
			bundle.putInt("price_type", price_type);
			intent.putExtras(bundle);
			intent.setClass(RoomListActivity.this, DishordersActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
			finish();
		}
		
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

	//	db = DBHelper.getDatabase(dbcm);
	}

}
